package com.fast.develop.dm.plugin.table.exporter;

import cn.hutool.core.util.ArrayUtil;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.model.relational.Namespace;
import org.hibernate.dialect.Dialect;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.Table;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author 莫寅
 * @createTime 2022-06-05 15:41
 * @desc：
 */

public class PluginTableExporter {

    private Dialect dialect;

    private Metadata metadata;

    public PluginTableExporter(Dialect dialect, Metadata metadata) {
        this.dialect = dialect;
        this.metadata = metadata;
    }


//    public PluginTableExporter(DomainSourceConfiguration domainSourceConfiguration) {
//        this.dialect = ((SessionFactoryImpl)domainSourceConfiguration.getSessionFactory()).getJdbcServices().getDialect();
//        this.metadata = domainSourceConfiguration.getMetadata();
//    }

    /**
     * Get the commands needed for creation.
     *
     * @return The commands needed for creation scripting.
     */
    public String[] getSqlCreateStrings(){
        List tableSqls = new ArrayList();
        for ( Namespace namespace : metadata.getDatabase().getNamespaces() ) {
            namespace.getTables().forEach(table -> {
                String[] sql = dialect.getTableExporter().getSqlCreateStrings( table, metadata );
                tableSqls.addAll(Arrays.asList(sql));

            });
        }

        return (String[]) tableSqls.toArray(new String[]{});
    }

    /**
     * Get the commands needed for creation.
     *
     * @return The commands needed for creation scripting.
     */
    public String getFormatSqlCreateStrings(){
        String[] sqls = getSqlCreateStrings();

        return ArrayUtil.join(sqls, ";\n");
    }


    /**
     * Get the commands needed for creation.
     *
     * @return The commands needed for creation scripting.
     */
    public String getFormatSqlCreateStrings(Table exportable){
        String[] sqls = getSqlCreateStrings(exportable);

        return ArrayUtil.join(sqls, ";\n");
    }

    /**
     * Get the commands needed for creation.
     *
     * @return The commands needed for creation scripting.
     */
    public String[] getSqlCreateStrings(Table exportable){

        String[] sqls = dialect.getTableExporter().getSqlCreateStrings( exportable, metadata );

        return sqls;
    }


    /**
     * Get the commands needed for creation.
     *
     * @return The commands needed for creation scripting.
     */
    public String getFormatSqlCreateStrings(String entityName){
        String[] sqls = getSqlCreateStrings(entityName);

        return ArrayUtil.join(sqls, ";\n");
    }

    /**
     * Get the commands needed for creation.
     *
     * @return The commands needed for creation scripting.
     */
    public String[] getSqlCreateStrings(String entityName){
        PersistentClass persistentClass = metadata.getEntityBinding(entityName);

        String[] sql = dialect.getTableExporter().getSqlCreateStrings( persistentClass.getTable(), metadata );

        return sql;
    }
}
